package com.aixin.project.servlet;

import com.aixin.project.data.*;
import com.aixin.project.handler.CourseHandler;
import com.aixin.project.handler.ExpertHandler;
import com.aixin.project.handler.UserHandler;
import com.aixin.project.util.AxMemcached;
import com.aixin.project.util.RequestUtil;
import org.apache.velocity.Template;
import org.apache.velocity.context.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

public class MycourseServlet extends BaseServlet {

    @Override
    protected Template execute(HttpServletRequest request,
                               HttpServletResponse response, Context ctx) {
        AxSession axsession = (AxSession) request.getSession().getAttribute(CommonConstants.USER_SESSION);
        if (null == axsession) {
            this.sendRedirect(response, "/");
            return null;
        }

        long id = axsession.getUid();
        UserType type = axsession.getUserType();


        if (type.equals(UserType.USER)) {
            System.out.println("usertype: user");

            String userName = RequestUtil.getStr(request, "userName", null);
            String password = RequestUtil.getStr(request, "password", null);

            if (userName != null && password != null) {
                User user = new User();
                String userSchool = RequestUtil.getStr(request, "schoolName", "");
                String conact = RequestUtil.getStr(request, "conact", "");
                String conactDetail = RequestUtil.getStr(request, "conactType", "");
                String email = RequestUtil.getStr(request, "email", "");
                user.setUsername(userName);
                user.setPassword(password);
                user.setContactperson(conact);
                user.setContactdetails(conactDetail);
                user.setContactemail(email);
                user.setContactschool(userSchool);
                int status = UserHandler.update(user);
                if (status == 1) {
                	AxMemcached.delete(CommonConstants.MKEY_USER+id);
                    System.out.println("已更新");
                }

            }
            HashMap<String, Integer> courseStatusMap = CourseHandler.courseStatusByUid(id);
            int finishCount = (null == courseStatusMap || null == courseStatusMap.get(CourseStatus.FINISH.toString())) ? 0 : courseStatusMap.get(CourseStatus.FINISH.toString());
            int nostarCount = (null == courseStatusMap || null == courseStatusMap.get(CourseStatus.NOSTART.toString())) ? 0 : courseStatusMap.get(CourseStatus.NOSTART.toString());


            HashMap<String, List<Course>> ordersMap = (HashMap<String, List<Course>>) CourseHandler.orderList(id);

            List<Course> unpayOrderList = ordersMap == null ? null : ordersMap.get(CourseStatus.UNPAYORDER.toString());
            List<Course> orderList = ordersMap == null ? null : ordersMap.get(CourseStatus.PAYORDER.toString());

            User user = UserHandler.find(id);
            ctx.put("user", user);
            ctx.put("finishCount", finishCount);
            ctx.put("nostarCount", nostarCount);

            ctx.put("unpayOrderList", unpayOrderList);
            ctx.put("orderList", orderList);

            request.setAttribute("navcourse", true);
            return getTemplate("mycourse.vm");

        } else if (type.equals(UserType.EXPERT)) {
            System.out.println("usertype: expert");

            HashMap<String, Integer> courseStatusMap = CourseHandler.courseStatusByEid(id);
            System.out.println("courseStatusMap finishe");
            System.out.println("courseStatusMap:"+(courseStatusMap==null?0:courseStatusMap.keySet().size()));

            int finishCount = (null == courseStatusMap || null == courseStatusMap.get(CourseStatus.FINISH.toString()))?0:courseStatusMap.get(CourseStatus.FINISH.toString());
            int nostarCount = (null == courseStatusMap || null == courseStatusMap.get(CourseStatus.NOSTART.toString()))?0:courseStatusMap.get(CourseStatus.NOSTART.toString());
            int liveCount = (null == courseStatusMap || null == courseStatusMap.get(CourseStatus.LIVE.toString()))?0:courseStatusMap.get(CourseStatus.LIVE.toString());

            HashMap<String, List<Course>> courseMap = CourseHandler.courseList(id);
            System.out.println("courseMap:"+(courseMap==null?0:courseMap.keySet().size()));

            List<Course> warnCourses = (courseMap==null?null:courseMap.get(CourseStatus.WARNCOURSE.toString()));

            List<Course> nostarCourses = (courseMap==null?null:courseMap.get(CourseStatus.NOSTARTCOURSE.toString()));

            System.out.println("finishCount : "+finishCount);
            System.out.println("nostarCount : "+nostarCount);
            System.out.println("liveCount : "+liveCount);
            System.out.println("warnCourses : "+warnCourses);
            System.out.println("nostarCourses : "+nostarCourses);

            Expert ex = ExpertHandler.find(id);
            ctx.put("expert", ex);
            ctx.put("finishCount", finishCount);
            ctx.put("nostarCount", nostarCount);
            ctx.put("liveCount", liveCount);
            ctx.put("warnCourse", warnCourses);
            ctx.put("nostarCourse", nostarCourses);
            request.setAttribute("navcourse", true);
            return getTemplate("excourse.vm");
        }

        return null;
    }
}
